package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/mitchellh/mapstructure"
	"log"
	"net/http"
	"strconv"
)

func GetAll(c *gin.Context) {
	num()
	R := Get()
	c.JSON(http.StatusOK, R)
}
func Update(c *gin.Context) {
	json := make(map[string]interface{}) //注意该结构接受的内容
	c.BindJSON(&json)
	book := Book{}
	err := mapstructure.Decode(json, &book)
	if err != nil {
		fmt.Println(err.Error())
	}
	log.Println(book)
	R2 := Save(book)
	c.JSON(http.StatusOK, R2)
}
func Put(c *gin.Context) {

	json := make(map[string]interface{}) //注意该结构接受的内容
	c.BindJSON(&json)
	book := Book{}
	log.Println(json)
	R2 := PutIn(json, book)
	c.JSON(http.StatusOK, R2)
}
func Del(c *gin.Context) {
	num()
	i := c.Param("id")
	log.Println(i)
	R := Dell(i)
	c.JSON(http.StatusOK, R)
}
func GeiById(c *gin.Context) {
	num()
	i := c.Param("id")
	log.Println(i)
	R := GetByIdd(i)
	c.JSON(http.StatusOK, R)
}
func GetPage(c *gin.Context) {
	num()
	N := c.Param("pageNum")
	M := c.Param("pageSize")
	log.Println(N, M)
	NINT, errN := strconv.Atoi(N)
	MINT, errM := strconv.Atoi(M)
	if errN != nil || errM != nil {
		log.Println("分页类型转换错误")
	}
	R := pages(NINT, MINT)
	c.JSON(http.StatusOK, R)
}
func GetPgeFind(c *gin.Context) {
	num()
	N := c.Param("pageNum")
	M := c.Param("pageSize")
	name := c.Query("name")
	NINT, errN := strconv.Atoi(N)
	MINT, errM := strconv.Atoi(M)
	if errN != nil || errM != nil {
		log.Println("分页类型转换错误")
	}
	R := page2(NINT, MINT, name)
	c.JSON(http.StatusOK, R)
}
